package com.itita.ww2.handler.requesthandler.army;

import java.sql.SQLException;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.itita.ww2.core.MessageHandler;
import com.itita.ww2.core.R;
import com.itita.ww2.core.WW2Exception;
import com.itita.ww2.game.user.UserService;
import com.itita.ww2.model.user.DevTime.DevTimeType;
import com.itita.ww2.model.user.DevTime;
import com.itita.ww2.model.user.UserProfile;
import com.itita.ww2.utils.handler.IRequestOper;

/**
 * 生产兵兵
 * @author NorthLan
 *
 */
public class ArmyProduce implements IRequestOper {

	@Override
	public boolean execute(String requestCMD, User user, ISFSObject params) throws WW2Exception, SQLException {
		UserProfile userProfile = UserService.getInstance().getUserProfile(user);
		String armyCode = params.getUtfString("code");// 单位代号 U001 U101 U201 ...
		int count = params.getInt("count"); // 生产数量
		// 
		ISFSObject backObj = SFSObject.newInstance();
		backObj.putInt(R.DATA_KEY_COUNTER, params.getInt(R.DATA_KEY_COUNTER));

		if (!userProfile.getArmyManager().armyProduce(armyCode, count)) {
			throw new WW2Exception();
		}

		DevTime dt = userProfile.getTimeManager().getDevTime(DevTimeType.PRODUCE, armyCode);
		ISFSObject ret = dt.toSFSObject();
		ret.putUtfString("key", dt.getKey());
		backObj.putSFSObject("devTime", ret);

		// 新手引导设置一下
		if (params.containsKey("guidestep")) {
			userProfile.getUserInfo().setGuideStep(params.getInt("guidestep"));
		}

		backObj.putUtfString("code", armyCode);
		backObj.putInt("pc", count);
		MessageHandler.getInstance().sendMsg(requestCMD, backObj, user, null, true);
		return true;
	}

}
